import i18n from '@/i18n'
import { MetaResourceChartDao } from '@apps/meta/domain/meta-resource-chart/meta-resource-chart.dao'
import { MetaResourceModelDao } from '@apps/meta/domain/meta-resource-model/meta-resource-model.dao'
import { DomainModel } from '@apps/meta/domain/model'
import { Assist } from '@libs/lhy-common'
import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { MetaResourceModelService } from '../meta-resource-model/meta-resource-model.service'
import { MetaResourceChartListDto } from './dto/meta-resource-chart-list.dto'
import { MetaResourceChartSaveDto } from './dto/meta-resource-chart-save.dto'
import { MetaResourceChartUpdateByIdDto } from './dto/meta-resource-chart-update-by-id.dto'

@ApiTags(i18n.t('app-meta.meta-resource-chart.meta-resource-chart.12151148-0'))
@Controller({
  path: 'resource_chart',
  version: '1',
})
export class MetaResourceChartController {
  constructor(
    private readonly reosurceChartDao: MetaResourceChartDao,
    private readonly resourceModelDao: MetaResourceModelDao,
    private readonly resourceModelService: MetaResourceModelService
  ) {}

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-chart.meta-resource-chart.12151148-1') })
  @Get('list')
  list(@Query() param: MetaResourceChartListDto) {
    return this.reosurceChartDao.list({
      ...param,
      label: {
        contains: param.label,
      },
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-chart.meta-resource-chart.12151148-2') })
  @Put()
  async save(@Body() param: MetaResourceChartSaveDto): Promise<void> {
    Assist.threw(
      await this.reosurceChartDao.isNameExists({ platform: param.platform, name: param.name }),
      i18n.t('app-meta.meta-resource-chart.meta-resource-chart.12151148-3', { args: [param.name] })
    )
    Assist.threw(
      await this.reosurceChartDao.isLabelExists({ platform: param.platform, label: param.label }),
      i18n.t('app-meta.meta-resource-chart.meta-resource-chart.12151148-4', { args: [param.label] })
    )

    const requestModels = this.resourceModelService.buildBatchSaveModels(param.requestModels || [], {
      type: DomainModel.MetaResourceModelType.Request,
      relativeType: DomainModel.MetaResourceRelativeType.Chart,
      relativeId: -1,
    })

    const newData = await this.reosurceChartDao.save(param)

    if (requestModels.length) {
      await this.resourceModelDao.batchSave(
        requestModels.map(model => ({
          ...model,
          relativeId: newData.id,
        }))
      )
    }
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-chart.meta-resource-chart.12151148-5') })
  @Post()
  async updateById(@Body() param: MetaResourceChartUpdateByIdDto): Promise<void> {
    const oldData = Assist.notNull(
      await this.reosurceChartDao.getById(param.id),
      i18n.t('app-meta.meta-resource-chart.meta-resource-chart.12151148-6', { args: [param.id] })
    )

    Assist.threw(
      await this.reosurceChartDao.isLabelExists({ platform: oldData.platform, label: param.label, id: param.id }),
      i18n.t('app-meta.meta-resource-chart.meta-resource-chart.12151148-4', { args: [param.label] })
    )

    const requestModels = this.resourceModelService.buildBatchSaveModels(param.requestModels || [], {
      type: DomainModel.MetaResourceModelType.Request,
      relativeType: DomainModel.MetaResourceRelativeType.Chart,
      relativeId: param.id,
    })

    await this.reosurceChartDao.updateById(param.id, param)
    await this.resourceModelDao.deleteByRelative({
      type: DomainModel.MetaResourceModelType.Request,
      relativeType: DomainModel.MetaResourceRelativeType.Chart,
      relativeId: param.id,
    })

    if (requestModels.length) {
      await this.resourceModelDao.batchSave(requestModels)
    }
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-resource-chart.meta-resource-chart.12151148-7') })
  @Delete()
  async deleteById(@Query('id') id: number): Promise<void> {
    await this.reosurceChartDao.deleteById(id, true)
  }
}
